package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.adapter.BankAdapter;
import com.pengcheng.taskapplication.adapter.CarAdapter;
import com.pengcheng.taskapplication.adapter.CarAdapterNew;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.helper.ImageLoader;
import com.pengcheng.taskapplication.model.CarModel;
import com.pengcheng.taskapplication.model.UrlItemModel;
import com.pengcheng.taskapplication.model.WalletModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZFT on 2020/10/27.
 */

public class MyCarList extends BaseActivity implements IView{
    private UserPresenter userPresenter;
    @BindView(R.id.pull_to_refresh_lv)
    PullToRefreshListView pullToRefreshListView;
    @BindView(R.id.img_show)
    ImageView img_show;
    @BindView(R.id.tv_center_title)
    TextView tv_center;

    private int page=1;
    private int pagesize=10;
    private String uid;
    private String newtoken;
    private boolean showAll;
    private String type="";


    ArrayList<CarModel> list =new ArrayList<>();
    private CarAdapterNew adapter;
    @Override
    protected int layoutResID() {
        return R.layout.activity_my_car_list_layout;
    }

    @Override
    protected void initData() {
        type=getIntent().getStringExtra("type");
        if (type.equals("1")){
            tv_center.setText("检验标识电子凭证");
        }else if(type.equals("2")){
            tv_center.setText("电子行驶证");
        }
        uid= SPUtils.getString(context, ConstantValue.SpType.userid);
        newtoken= SPUtils.getString(context,ConstantValue.SpType.newtoken);
        userPresenter = new UserPresenter(this);
        //adapter
        adapter = new CarAdapterNew(context,list,type);
        //manager
        pullToRefreshListView.setAdapter(adapter);

        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //用车牌号去换取url
                CarModel model=(CarModel) adapterView.getAdapter().getItem(i);
                if(type.equals("1")){
                    getNianJianUrl(model.getVehicleId());
                }else if(type.equals("2")){
                    getXSZUrl(model.getVehicleId());
                }


            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page=0;
                list.clear();
                getCars(page,pagesize,showAll);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getCars(page,pagesize,showAll);
            }
        });

        pullToRefreshListView.setEmptyView(findViewById(R.id.empty_view));
        getCars(page,pagesize,showAll);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {

        if(actionType.equals(ConstantValue.RequestKey.getCars)){
            list.addAll((ArrayList<CarModel>)data);
            adapter.notifyDataSetChanged();
            pullToRefreshListView.onRefreshComplete();
        }
        if(actionType.equals(ConstantValue.RequestKey.getNianJianUrl)){
            UrlItemModel urlItemModel=(UrlItemModel)data;
            Intent intent =new Intent(context,Html5Activity.class);
            intent.putExtra("url",urlItemModel.getUrl());
            intent.putExtra("title","检验标识电子凭证");
            startActivity(intent);
        }

        if(actionType.equals(ConstantValue.RequestKey.getxszUrl)){
            UrlItemModel urlItemModel=(UrlItemModel)data;
            Intent intent =new Intent(context,Html5Activity.class);
            intent.putExtra("url",urlItemModel.getUrl());
            intent.putExtra("title","电子行驶证");
            startActivity(intent);
        }

    }

    @Override
    public void onFailed(String code, String msg) {
        pullToRefreshListView.onRefreshComplete();
        ToastUtils.showShortToast(msg);
    }

    @OnClick({R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
        }
    }


    public void getYuCheBaoSubscribeUrl(String vehicleId){
        userPresenter.getYuCheBaoSubscribeUrl(vehicleId,ConstantValue.RequestKey.getYuCheBaoSubscribeUrl);
    }

    public void getNianJianUrl(String vehicleId){
        userPresenter.getNianJianUrl(vehicleId,ConstantValue.RequestKey.getNianJianUrl);
    }

    public void getXSZUrl(String vehicleId){
        userPresenter.getXSZUrl(vehicleId,ConstantValue.RequestKey.getxszUrl);
    }

    public void getCars(int page,int pageSize,boolean showAll){
        userPresenter.getCars(page,pageSize, showAll,ConstantValue.RequestKey.getCars);
    }
}
